/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.commons;

import java.util.logging.Level;
import org.balisunrise.application.Application;
import org.balisunrise.builder.FactoryException;
import org.balisunrise.common.Query;
import org.balisunrise.common.Criterionable;
import org.balisunrise.common.EntityLoader;
import org.balisunrise.common.VisualEntityType;
import org.balisunrise.common.abstracts.AbstractEntityFilter;
import org.balisunrise.swing.components.panel.FieldsPanel;

/**
 *
 * @author Glauco
 */
public class EntityFilter extends AbstractEntityFilter
        implements org.balisunrise.common.EntityFilter<FieldsPanel>{


    public static final String PROP_VISUAL_REPRESENTATION =
            "visualRepresentation";

    private FieldsPanel visualRepresentation;

    @Override
    public void setEntityLoader(EntityLoader entityLoader) {
        super.setEntityLoader(entityLoader);
    }

    public void setVisualRepresentation(FieldsPanel visualRepresentation) {
        FieldsPanel old = this.visualRepresentation;
        this.visualRepresentation = visualRepresentation;
        changeSupport.firePropertyChange(PROP_VISUAL_REPRESENTATION,
                old, visualRepresentation);
    }

    @Override
    public Query getQuery() {
        try{
            Query query = Application.get().getFactory()
                    .createBaliQuery(entityClass);
            query.setOwner(this);
            for (org.balisunrise.common.FieldLayout fl : layouts) {
                if(fl instanceof Criterionable){
                    Criterionable c = (Criterionable) fl;
                    query.addCriterion(c.getCriterion());
                }
            }
            return query;
        } catch (FactoryException fex){
            Application.get().getLogger().log(Level.SEVERE,
                    "EntityFilter não pode criar Query: " +
                    fex.getMessage(), fex);
        }
        return null;
    }

    /**
     * Retorna a representação visual.
     */
    @Override
    public FieldsPanel getVisualRepresentation() {
        return visualRepresentation;
    }

    /**
     * Retorna o tipo do VisualEntity.<BR>
     * Sempre retornará ENTITY_FILTER.
     * @return VisualEntityType.ENTITY_FILTER
     */
    @Override
    public VisualEntityType getVisualEntityType() {
        return VisualEntityType.ENTITY_FILTER;
    }

    /**
     * Verifica se algum Criterion foi modificado depois da chamada getQuery().
     */
    @Override
    public boolean isModified() {
        for (org.balisunrise.common.FieldLayout fl : layouts) {
                if(fl instanceof Criterionable){
                    Criterionable c = (Criterionable) fl;
                    if(c.isModified())
                        return true;
                }
            }
        return false;
    }

}
